//@version=5

import DevLucem/ZigLib/1 as ZigZag
indicator('S.S.S.R', 'S.S.S.R', true, format.price, max_labels_count=200, max_lines_count=50)

Depth = 24
Deviation = 5
Backstep = 2
line_thick = 2
labels = 0
upcolor = color.lime
dncolor = color.red
lines = 60
background = 100
label_size = size.normal
repaint = true
extend = false

[direction, z1, z2] = ZigZag.zigzag(low, high, Depth, Deviation, Backstep)
string nowPoint = ""
var float lastPoint = z1.price[1]
if bool(ta.change(direction))
    lastPoint := z1.price[1]

line zz = na
label point = na

if repaint
    zz := line.new(z1, z2, xloc.bar_time, extend ? extend.right : extend.none, color.new(direction > 0 ? upcolor : dncolor, lines), width=line_thick)
    nowPoint := direction < 0 ? (z2.price < lastPoint ? "buy" : "buy") : (z2.price > lastPoint ? "sell" : "sell")
    point := label.new(z2, nowPoint, xloc.bar_time, yloc.price, 
     color.new(direction < 0 ? upcolor : dncolor, labels), direction > 0 ? label.style_label_down : label.style_label_up, color.new(direction > 0 ? upcolor : dncolor, labels), label_size)
    if direction == direction[1]
        line.delete(zz[1])
        label.delete(point[1])
    else
        line.set_extend(zz[1], extend.none)
else
    if direction != direction[1]
        zz := line.new(z1[1], z2[1], xloc.bar_time, extend.none, color.new(direction > 0 ? upcolor : dncolor, lines), width=line_thick)
        nowPoint := direction[1] < 0 ? (z2.price[1] < lastPoint[1] ? "buy" : "buy") : (z2.price[1] > lastPoint[1] ? "sell" : "sell")
        point := label.new(z2[1], nowPoint, xloc.bar_time, yloc.price, 
         color.new(direction[1] < 0 ? upcolor : dncolor, labels), direction[1] > 0 ? label.style_label_down : label.style_label_up, color.new(direction[1] > 0 ? upcolor : dncolor, labels), label_size)
bgcolor(direction < 0 ? color.new(dncolor, background) : color.new(upcolor, background), title='Direction Background')
plotarrow(direction, "direction", display=display.status_line)

alertcondition(nowPoint == "sell" and z2.price != z2.price[1], "New Higher High", 'Zigzag on {{ticker}} higher high detected at {{time}}')
alertcondition(nowPoint == "buy" and z2.price != z2.price[1], "New Lower High", 'Zigzag on {{ticker}} lower high detected at {{time}}')
alertcondition(nowPoint == "buy" and z2.price != z2.price[1], "New Higher Low", 'Zigzag on {{ticker}} higher low detected at {{time}}')
alertcondition(nowPoint == "buy" and z2.price != z2.price[1], "New Lower Low", 'Zigzag on {{ticker}} lower low detected at {{time}}')
alertcondition(direction != direction[1], 'Direction Changed', 'Zigzag on {{ticker}} direction changed at {{time}}')
alertcondition(direction != direction[1] and direction > 0, 'Bullish Direction', 'Zigzag on {{ticker}} bullish direction at {{time}}')
alertcondition(direction != direction[1] and direction < 0, 'Bearish Direction', 'Zigzag on {{ticker}} bearish direction at {{time}}')

if direction != direction[1]
    alert((direction < 0 ? "Bearish" : "Bullish") + " Direction Final ", alert.freq_once_per_bar_close)